﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using University.Workshop.Model;
using University.Workshop.DataAccess;
using University.Workshop.Business;

namespace University.Workshop.WebApp
{
    public partial class MyCredit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            User user = UserDataAccess.GetUserByEmail(User.Identity.Name);
            bool hasNewNotification = GiftCardManager.UserHasGiftCard(user);
            GiftCard giftcard = GiftCardManager.GetGiftCardByRecipientId(user.Id);
            
            if (hasNewNotification)
            {
                GiftCardManager.ChangeNotificationToYes(giftcard);
                User senderUser = GiftCardManager.GetSenderUser(giftcard.UserIdSender);
                Response.Redirect("GiftCardNotification.aspx?Quantity=" + giftcard.Quantity + "&Currency=" + giftcard.Currency + "&SenderName=" + senderUser.PersonalInformation.FirstName + "&SenderLastName=" + senderUser.PersonalInformation.LastName);
            }
            else
            {
                if (giftcard != null)
                {
                    LoadGridView(giftcard.GiftCardId);
                    GridViewRow row = GridView1.Rows[0];
                    this.QuantityLabel.Text = row.Cells[0].Text;
                    this.CurrencyLabel.Text = row.Cells[1].Text;
                }
                else
                    EmptyCredit();
                
            }
        }

        protected void EmptyCredit()
        {
            this.QuantityLabel.Text = "0";
            this.CurrencyLabel.Text = "Bs./$us";
        }
        protected void LoadGridView(long giftCardId)
        {
            GridView1.DataSource = GiftCardManager.GetGiftCardQuantityCurrency(giftCardId);
            GridView1.DataBind();
        }
    }
}